/*=======================================*/
/*	Alan Liu                             */
/*	email:mingliu@bizconf.cn	         */
/*	2013-07-23                           */
/*=======================================*/

/* Debug Utility */
function logDebug(message) {
	if (window.console && window.console.debug) {
        window.console.log(message);
    }
}

function getUrlParams(){
  var params = [], hash;
  var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  for(var i = 0; i < hashes.length; i++)
  {
    hash = hashes[i].split('=');
    params.push(hash[0]);
    var value = hash[1];
    if (value && value.indexOf('#')>=0) {
    	value = value.substring(0, value.indexOf('#'));
    }
    params[hash[0]] = decodeURIComponent(value);
  }
  return params;
}
function getUrlParam(name){
  return getUrlParams()[name];
}
function addUrlParam(url, paramName, paramValue, encode) {
    if (paramName && paramValue && url.indexOf(paramName+"=")<0) {
        url += url.indexOf("?")>=0?"&":"?";
        if(encode){
            url += paramName + "=" + encodeURIComponent(encodeURIComponent(paramValue));	
        } else {
        	url += paramName + "=" + paramValue;
        }
    }
    return url;
}

function addT(url, time) {
    if (url && time!=-1) {
        var t = time;
        if (!t) {
            t = new Date().getTime();
        }
        url = addUrlParam(url, "t", t);
    }
    return url;
}
/* Ajax Methods */
function ajaxGet(url, data, sucessCallback, failureCallback, options) {
    ajaxLoad("get", url, data, sucessCallback, failureCallback, options);
}
function ajaxPost(url, data, sucessCallback, failureCallback, options) {
    ajaxLoad("post", url, data, sucessCallback, failureCallback, options);
}
function ajaxLoad(type, url, data, sucessCallback, failureCallback, options) {
    var postloading = null;
    var pageLoading = "true";
    if(options && options.pageLoading && options.pageLoading == "false") {
    	pageLoading = "false";
    }
    var dataType = "text";
    $.ajax({
        type: type,
        url: url,
        data: data,
        beforeSend: function(XMLHttpRequest) {
            if (pageLoading=="true" && options && options.message) {
                postloading = showPostLoading(options.message, options.ui);
            }
        },
        success: function(data, textStatus, XmlHttpRequest) {
            if (pageLoading=="true" && postloading) {
                hidePostLoading(postloading);
            }
            if (sucessCallback) {
    			if(dataType=="text" || dataType=="json"){
    				try {
        				data = JSON.parse(data);	
					} catch (e) {
						
					}
    			}	
                sucessCallback(data);
            }
        },
        error: function(XmlHttpRequest, textStatus, errorThrown) {
            if (pageLoading=="true") {
                hidePostLoading(postloading);
            }
        }
    });
}

/*获取最上层document*/
function getTopDoc() {
  if (window.top!=window.self){
  	return window.top.document;
  } else {
  	return window.document;
  }
}
/*获取最上层窗口可见宽度和高度*/
function getTopScrSize() {
  return {
    width: getTopDoc().documentElement.clientWidth,
    height: getTopDoc().documentElement.clientHeight
  };
}
/*获取最上层文档的宽度和高度*/
function getTopDocSize() {
  return {
    width : getTopDoc().body.clientWidth,
    height: getTopDoc().body.clientHeight
  };
}
/*获取滚动条的高度*/
function getScrollTop()
{
    var scrollTop=0;
    if(getTopDoc().documentElement&&getTopDoc().documentElement.scrollTop)
    {
        scrollTop=getTopDoc().documentElement.scrollTop;
    }
    else if(getTopDoc().body)
    {
        scrollTop=getTopDoc().body.scrollTop;
    }
    return scrollTop;
}
/*显示加载数据提示*/
function showPostLoading(message, ui) {
    var doc = ui.document;
    var containerId = "load-container";
    var container = doc.getElementById(containerId);
    if (!container) {
        container = doc.createElement("div");
        container.id = containerId;
        var docWdith = getTopDocSize().width;
        var docHeight = getTopDocSize().height;
        var screenHeight = getTopScrSize().height;
        var scrollTop = getScrollTop();
        //遮罩
        var overLay = doc.createElement("div");
        overLay.style.position = "absolute";
        overLay.style.zIndex = "2000";
        overLay.style.width =  docWdith + "px";
        overLay.style.height =  docHeight + "px";
        overLay.style.top = "0px";
        overLay.style.left = "0px";
        overLay.style.background = "#AAAAAA";
        overLay.style.filter = "alpha(opacity=30)";
        overLay.style.opacity = "0.3";
        container.appendChild(overLay);
        //内容
        var content = doc.createElement("div");
        content.style.position = "absolute";
        content.style.zIndex = "2002";
        content.style.left =  docWdith/2 + "px";
        content.style.top = scrollTop + screenHeight/2 + "px";
        var loadingDiv = doc.createElement("div");
        loadingDiv.style.width = "32px";
        loadingDiv.style.height = "32px";
        loadingDiv.style.background = "#FFFFFF";
        loadingDiv.style.cssFloat = "left";//styleFloat for ie
        var loadingImg = doc.createElement("img");
        loadingImg.src = "/static/img/loading.gif";
        var contentText = doc.createElement("div");
        contentText.id = "loadMsg";
        contentText.innerHTML = message;
        contentText.style.height = "32px";
        contentText.style.lineHeight = "32px";
        contentText.style.cssFloat = "left";//styleFloat for ie
        contentText.style.marginLeft = "15px";
        loadingDiv.appendChild(loadingImg);
        content.appendChild(loadingDiv);
        content.appendChild(contentText);
        container.appendChild(content);
        ui.document.body.appendChild(container);
    } else {
    	var loadMsg = doc.getElementById("loadMsg");
    	loadMsg.innerHTML = message;
    }
    container.style.display = "block";
    return container;
}

/*隐藏加载数据提示*/
function hidePostLoading(loading) {
	loading.style.display = "none";
}
var app = {
	/*
	 * 企业用户登录
	 */
	userLogin: function(data, callback, options) {
		var url = "/user/login/check";
		ajaxPost(url, data,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, {pageLoading: "false"});			
	},
	forgotUserPass: function(authCode, type, random, userEmail, callback) {
		var url = "/user/password/sendEmail";
		ajaxPost(url, {authCode:authCode, type: type, random:random, userEmail:userEmail},
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, {pageLoading: "false"});		
	},
	resetUserPass: function(params, callback) {
		var url = "/user/password/save";
		ajaxPost(url, params,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, {pageLoading: "false"});		
	},
	/*
	 * 系统前缀sysXXX 
	 */
	bookMeeting: function(data, callback, options) {
		var url = "/user/conf/createNewReservationConf";
		ajaxPost(url, data,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, options);	
	},
	updateBookMeeting: function(data, callback, options) {
		var url = "/user/conf/updateConfInfo";
		ajaxPost(url, data,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, options);		
	},
	sysUserLogin: function(data, callback, options) {
		var url = "/system/login";
		ajaxPost(url, data,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, options);			
	},
	sysForgotPass: function(data, callback, options) {
		var url = "/system/forgotpass";
		ajaxPost(url, data,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, options);
	},
	testJson: function() {
    	var url = "/system/thumbnail";
        ajaxGet(url, null,
                function(result) {
        			console.log(result);
        			if(callback) {
        				callback(result);
        			}
                }, null, null);  
		
	},
	addContact: function(data, callback) {
		var url = "/user/contact/saveSingle";
		ajaxPost(url, data,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, null);		
	},
	addContacts: function(data, callback, options) {
		var url = "/user/contact/importBatchByContacts";
		ajaxPost(url, data,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, options);		
	},
	addGroup: function(data, callback) {
		var url = "/user/group/save";
		ajaxPost(url, data,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, null);		
	},
	delContactFromGroup: function(id, groupId, callback, options) {
		var url = "/user/group/delContactsFormGroup?id="+id+"&group_id="+groupId;
		ajaxPost(url, null,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, options);		
	},
	remindConfUser: function(confId, callback, options) {
		var url = "/user/contact/remindConfUser";
		ajaxPost(url, {confId:confId},
				function(result) {
			if (callback) {
				callback(result);
			}
		}, null, options);
	},
	inventContact: function(param, callback, options) {
		var url = "/user/contact/sendinvites";
		ajaxPost(url, {data:JSON.stringify(param)},
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, options);
	},
	getLevelOrg: function(id, callback) {
		var url = "/user/contact/orgListForJson/"+id;
		ajaxGet(url, null,
                function(result) {
					if (callback) {
		                callback(result);
		            }
                }, null, null);
	}
};


String.prototype.trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
};

String.prototype.Trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
};
String.prototype.isEmpty = function() {
	if (this == null || this.trim().length == 0) {
		return true;
	} else {
		return false;
	}
};
String.prototype.isDigit = function() {
    if (this.isEmpty()) {
        return false;
    }
    var regex = /^\d+$/;
    return regex.test(this);
};

function getMainDomain(){
	var domain=document.domain;
	if(domain!=null && domain.length>0){
		var firstDotIndex=domain.indexOf(".");
		domain=domain.substr(firstDotIndex+1);
	}
	return domain;
}

function getDomain(){
	return document.domain;
}
function getCookieByDomain(cName,domain){
	var valueSplit="${COOKIE_SPLIT}";
	var cookie=null;
	var cValue=null;
	if(cName!=null && cName.trim()!=""){
		cookie=document.cookie;
		var cookieArray=cookie.split(";");
		if(cookieArray!=null && cookieArray.length>0){
			var eachCookie=null;
			var eachCookieArray=null;
			var eachName=null;
			var eachValue=null;
			var eachDomain=null;
			var eachTrueValue=null;
			var eachValueArray=null;
			for(var ii=0;ii<cookieArray.length;ii++){
				if(cValue!=null && cValue.trim()!=""){
					break;
				}
				eachCookie=cookieArray[ii];
				if(eachCookie!=null && eachCookie.trim()!=""){
					eachCookieArray=eachCookie.split("=");
					if(eachCookieArray!=null && eachCookieArray.length==2){
						eachName=eachCookieArray[0];
						if(eachName.trim()==cName){
							eachValue=eachCookieArray[1];
							if(domain==null && domain.trim()==""){
								cValue=eachValue;
							}else{
								if(eachValue!=null && eachValue.trim()!=""){
									 eachValueArray=eachValue.split(valueSplit);
								}
								if(eachValueArray!=null && eachValueArray.length==2){
									eachTrueValue=eachValueArray[0];
									eachDomain=eachValueArray[1];
									if(domain==eachDomain.trim()){
										cValue=eachTrueValue;
									}
								}
							}
						}
					}
				}
				eachCookie=null;
				eachCookieArray=null;
				eachName=null;
				eachValue=null;
				eachDomain=null;
				eachTrueValue=null;
				eachValueArray=null;
			}
		}
		
	}
	
	return cValue;
}



function setCookie(cName, cValue, domain) {
	if (!cName.isEmpty() && !cValue.isEmpty()) {
		var cookieSplit="${COOKIE_SPLIT}";
		//var timeStamp = (new Date()).getTime();
		//timeStamp += (hours * 3600 * 1000);
		//var expireDate = new Date(timeStamp).toGMTString();
		var saveValue = cValue;
		if (!domain.isEmpty()) {
			saveValue = cValue + cookieSplit + domain;
		}
		document.cookie = cName + "=" +  saveValue  + ";path=/;domain=" + domain + ";expires=";// + expireDate;
		timeStamp = null;
		saveValue = null;
		//expireDate = null;
	}
}
function clearCookie(cName, domain) {
	if (!cName.isEmpty()) {
		var timeStamp = (new Date()).getTime();
		timeStamp -= 1000;
		var expireDate = new Date(timeStamp).toGMTString();
		var saveValue = "";
		if (!domain.isEmpty()) {
			document.cookie = cName + "=" + saveValue + ";path=/;domain=" + domain + ";expires=" + expireDate;
		} else {
			document.cookie = cName + "=" + saveValue + ";path=/;expires=" + expireDate;
		}
		timeStamp = null;
		saveValue = null;
		expireDate = null;
	}
}


function getBrowserLang() {
	var domain = getMainDomain();
	var baseLang = getCookieByDomain("LANG",domain);
	if (!baseLang) {
		if (navigator.userLanguage) {
			baseLang = navigator.userLanguage.substring(0,2).toLowerCase();
		} else {
			baseLang = navigator.language.substring(0,2).toLowerCase();
		}
		/* language match */
		switch(baseLang)
		{
			case "en":
				/* english */
				baseLang = "en-us";
				break;
			case "zh":
				/* ${LANG['bizconf.jsp.common.cookie_util.res3']} */
				baseLang = "zh-cn";
				break;
			default:
				/* default no match */
		}
	}
	return baseLang;
}	


/*!
 * jQuery Cookie Plugin v1.3.0
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2013 Klaus Hartl
 * Released under the MIT license
 */
(function(g,b,h){var a=/\+/g;
function e(i){return i;}function c(i){return f(decodeURIComponent(i.replace(a," ")));}function f(i){if(i.indexOf('"')===0){i=i.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\");
}return i;}var d=g.cookie=function(r,q,w){if(q!==h){w=g.extend({},d.defaults,w);if(q===null){w.expires=-1;}if(typeof w.expires==="number"){var s=w.expires,v=w.expires=new Date();
v.setDate(v.getDate()+s);}q=d.json?JSON.stringify(q):String(q);return(b.cookie=[encodeURIComponent(r),"=",d.raw?q:encodeURIComponent(q),w.expires?"; expires="+w.expires.toUTCString():"",w.path?"; path="+w.path:"",w.domain?"; domain="+w.domain:"",w.secure?"; secure":""].join(""));
}var j=d.raw?e:c;var u=b.cookie.split("; ");var x=r?null:{};for(var p=0,n=u.length;p<n;p++){var o=u[p].split("=");var k=j(o.shift());var m=j(o.join("="));
if(d.json){m=JSON.parse(m);}if(r&&r===k){x=m;break;}if(!r){x[k]=m;}}return x;};d.defaults={};g.removeCookie=function(j,i){if(g.cookie(j)!==null){g.cookie(j,null,i);
return true;}return false;};})(jQuery,document);